const Flatten = require("../template")

function testFlattenBasic() {
  console.log(
    "1. 基本扁平化测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5]]
      const result = Flatten.flatten(arr)
      const expected = [1, 2, 3, 4, 5]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "2. 深度扁平化测试:",
    (() => {
      const arr = [1, [2, [3, [4, 5]]]]
      const result = Flatten.flattenDeep(arr)
      const expected = [1, 2, 3, 4, 5]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "3. 指定深度扁平化测试:",
    (() => {
      const arr = [1, [2, [3, [4, 5]]]]
      const result = Flatten.flattenDepth(arr, 2)
      const expected = [1, 2, 3, [4, 5]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "4. 空数组扁平化测试:",
    (() => {
      const arr = []
      const result = Flatten.flatten(arr)
      return result.length === 0
    })()
  )

  console.log(
    "5. 单元素数组扁平化测试:",
    (() => {
      const arr = [1]
      const result = Flatten.flatten(arr)
      const expected = [1]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "6. 无嵌套数组扁平化测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const result = Flatten.flatten(arr)
      const expected = [1, 2, 3, 4, 5]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "7. 字符串数组扁平化测试:",
    (() => {
      const arr = ['a', ['b', 'c'], ['d', 'e']]
      const result = Flatten.flatten(arr)
      const expected = ['a', 'b', 'c', 'd', 'e']
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "8. 对象数组扁平化测试:",
    (() => {
      const arr = [
        { id: 1, name: 'A' },
        [{ id: 2, name: 'B' }, { id: 3, name: 'C' }]
      ]
      const result = Flatten.flatten(arr)
      return result.length === 3 && result[0].id === 1
    })()
  )

  console.log(
    "9. 不可变扁平化测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5]]
      const result = Flatten.flattenImmutable(arr)
      const original = [1, [2, 3], [4, 5]]
      return JSON.stringify(arr) === JSON.stringify(original) && result.length === 5
    })()
  )

  console.log(
    "10. 零深度扁平化测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5]]
      const result = Flatten.flattenDepth(arr, 0)
      const expected = [1, [2, 3], [4, 5]]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "11. this上下文扁平化测试:",
    (() => {
      const flattenObj = {
        multiplier: 2,
        processFlatten: function(arr) {
          const flattened = Flatten.flatten(arr)
          return flattened.length * this.multiplier
        }
      }
      const arr = [1, [2, 3], [4, 5]]
      return flattenObj.processFlatten(arr) === 10 // 5*2=10
    })()
  )

  console.log(
    "12. 复杂对象this绑定扁平化测试:",
    (() => {
      const flattenConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(arr) {
          const flattened = Flatten.flatten(arr)
          return flattened.length >= this.config.threshold
        }
      }
      const arr = [1, [2, 3], [4, 5]]
      return flattenConfigObj.processWithConfig(arr) === true
    })()
  )
}

testFlattenBasic()
